package com.dk.controller;

import com.alibaba.fastjson.JSONObject;
import com.dk.VO.TbUserVO;
import com.dk.constant.SeckillConstant;
import com.dk.enums.ResultEnum;
import com.dk.pojo.TbSeckillCar;
import com.dk.result.Result;
import com.dk.service.TbSeckillCarService;
import com.dk.util.JwtUtil;
import com.xxl.job.core.handler.annotation.XxlJob;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api
@ApiOperation("秒杀接口")
@RestController
@RequestMapping("seckillCar")
public class TbSeckillCarController {


    @Autowired
    private TbSeckillCarService seckillCarService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private JwtUtil jwtUtil;


    @PostMapping ("prepareHot")
//    @XxlJob(value = "SeckillHot")
    @ApiOperation(value = "缓存预热")
    public Result prepareHot(){
        seckillCarService.prepareHot();
        System.out.println("缓存预热" + new Date());
        return Result.OK();
    }


    @PostMapping("getSeckillList")
    @ApiOperation(value = "查询限时抢购商品")
    public Result getSeckillList(){
        List<TbSeckillCar> seckillCars = seckillCarService.getSeckillList();
        for (TbSeckillCar car : seckillCars) {
            Long id = car.getId();
            Integer i = Integer.parseInt(stringRedisTemplate.boundValueOps(SeckillConstant.SECKILL_GOODS + id).get());
            if (i != null){
                car.setStockCount(i);
            }
        }
        return Result.OK().setData(seckillCars);
    }


    @PostMapping("createOrder")
    @ApiOperation(value = "生成订单，扣减库存")
    @ApiImplicitParam(name = "id", value = "订单主键id")
    public Result createOrder(Long id,@RequestHeader("AuthToken")String token){
        Claims claims = jwtUtil.parseJwt(token);
        TbUserVO user = JSONObject.parseObject(claims.getSubject(), TbUserVO.class);
        if (user == null){
            return Result.respones(ResultEnum.NO_LOGIN);
        }
        try {
            seckillCarService.createOrder(user,id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.OK();
    }


    @RequestMapping("getOrderId")
    @ApiOperation(value = "根据登录用户查询订单")
    public Result getOrderId(@RequestHeader("AuthToken")String token){
        Claims claims = jwtUtil.parseJwt(token);
        TbUserVO user = JSONObject.parseObject(claims.getSubject(), TbUserVO.class);
        if (user == null){
            return Result.respones(ResultEnum.NO_LOGIN);
        }
        Long orderId = seckillCarService.getOrderId(user.getUsername());
        if (orderId == null){
            return Result.ER();
        }
        return Result.OK().setData(orderId.toString());
    }


}
